var app = getApp();
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: '37BBZ-IASRP-7VZDT-V6FJE-USUFE-OVFGR' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigator: { hide: 0 },
    tab_index: 0,
    isBottom: 0,
    tabIndex: 0,
    topic_list: [],
    is_gift: false,
    pageNum: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (!app.globalData.userInfo) {
      app.getUserInfo();
    }
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })

    if(that.data.id){
      var id = that.data.id;
    }else{
      var id = options.id;
    }
    
    that.setData({
      id:id
    })
//定位
    wx.getLocation({
      type: 'wgs84',
      complete: function (resq) {
        console.log(resq);
        that.setData({
          laNum: resq.latitude,
          loNum: resq.longitude
        })
        let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
        app.ihttpGet('https://www.58zltc.com/api.php/Quan/circle_detail/id/' + id + '/openid/' + app.globalData.openid + '/la/' + that.data.laNum + '/lo/' + that.data.loNum, function (res) {
          that.setData({
            info: res.data,
            list: res.data.list
          })
        });
      }
    })

    
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/giftList', function (res) {
      console.log(res)
      that.setData({
        gift_list: res.data
      })
    });
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  follow: function (e) {
    var openid = app.globalData.openid;
    var id = e.currentTarget.dataset.id;
    var t = this
    app.ihttpGet("https://www.58zltc.com/api.php/Quan/circle_follow/openid/" + openid + "/id/" + id, function (res) {
      if (res.data.err_code) {
        wx.showToast({
          icon: "none",
          title: res.data.err_msg,
        })
        
      } else {
        wx.showToast({
          title: res.data.err_msg,
        })
        setTimeout(function () {
          t.onLoad()
        }, 1000)
      }
      wx.hideLoading();
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  goDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(e.currentTarget.dataset.video);
    if (e.currentTarget.dataset.video) {
      var url = '/pages/quan/video/video?id=' + id;
    } else {
      var url = '/pages/quan/detail?id=' + id;
    }
    wx.navigateTo({
      url: url,
    })
  },
  goMember: function (e) {
    var openid = e.currentTarget.dataset.openid;
    var url = '/pages/quan/member/member?openid=' + openid;
    wx.navigateTo({
      url: url,
    })
  },
  gift: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      is_gift: true,
      gift_id: id
    })
  },
  hiddenGift: function (e) {
    this.setData({
      is_gift: false
    })
  },
  setGift: function (e) {
    var idx = e.currentTarget.dataset.idx;
    let that = this;
    var gift_list = that.data.gift_list;
    gift_list.forEach(function (value, key) {
      gift_list[key].active = key == idx ? true : false;
    })
    this.setData({
      gift_list: gift_list,
      active_gift: gift_list[idx]
    })
  },
  payGift: function (e) {
    let that = this;
    var data = this.data;
    var post_data = {};

    data.list.forEach(function (value, idx) {
      if (value.id == data.gift_id) {
        post_data.to = value.follow_openid;
      }
    })
    post_data.type = 'quan';
    post_data.from = app.globalData.openid;
    post_data.id = data.gift_id;
    post_data.gift_id = data.active_gift.id;

    app.ihttpPost('https://www.58zltc.com/api.php/Quan/payGift', post_data, function (res) {
      if (res.data.err_code == 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '赠送成功！',
              })
            }
          }
        })
        that.reBg();
      }
    })
  },
  zan: function (e) {
    
    var id = e.currentTarget.dataset.id;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    var list = this.data.list;
    var that = this;
    wx.request({
      url: 'https://www.58zltc.com/api.php/Quan/zan?openid=' + app.globalData.openid + '&id=' + id + '&token=' + extConfig.appid,
      success: res => {
        if (res.data.err_code === 0) {
          list.forEach(function (vo, idx) {
            if (vo.id == id) {
              if (res.data.err_msg == '-1') {
                list[idx].zan_num = parseInt(list[idx].zan_num) - 1;
                list[idx].is_zan = false;
              } else {
                list[idx].zan_num = parseInt(list[idx].zan_num) + 1;
                list[idx].is_zan = true;
              }
            }
          })
          that.setData({
            list: list
          })
         
        } else {
          wx.showToast({
            title: res.data.err_msg
          })
         
        }
      }
    })
  },
  showImgs: function (e) {
    const pics = e.currentTarget.dataset.pics;
    const first_pics = e.currentTarget.dataset.image;
    wx.previewImage({
      urls: pics,
      current: first_pics
    })
  },
  changeTab: function (e) {
    this.setData({
      tab_index: e.currentTarget.dataset.tab_index
    })
  }
})